﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FormDuctInsulationLayer : System.Windows.Forms.Form
	{
		public FormDuctInsulationLayer()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		public double GetMediumTemp()
		{
			return this.GetSpecTemp(this.hyTxtMediumTemp);
		}

		public double GetDesignTemp()
		{
			return this.GetSpecTemp(this.hyTxtDesignTemp);
		}

		public double GetDewingTemp()
		{
			return this.GetSpecTemp(this.hyTxtDewingTemp);
		}

		public InsulationLayerParam GetSettingMaterial()
		{
			InsulationLayerParam result = null;
			string text = this.cmbInsulationMaterial.Text;
			List<InsulationLayerParam> list = this.cmbInsulationMaterial.Tag as List<InsulationLayerParam>;
			if (this.tabControl1.SelectedIndex == 1)
			{
				text = this.cmbDefineInsulationMaterial.Text;
				list = (this.cmbDefineInsulationMaterial.Tag as List<InsulationLayerParam>);
			}
			foreach (InsulationLayerParam insulationLayerParam in list)
			{
				result = insulationLayerParam;
				if (insulationLayerParam.Name.Equals(text))
				{
					result = insulationLayerParam;
					break;
				}
			}
			return result;
		}

		public double GetDefineThickness()
		{
			double result;
			try
			{
				result = Convert.ToDouble(this.hyTxtDefineDepth.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = 0.0;
			}
			return result;
		}

		public double GetInsulationThickness()
		{
			double result = 0.0;
			try
			{
				if (this.tabControl1.SelectedIndex == 1)
				{
					result = Convert.ToDouble(this.hyTxtDefineDepth.Text);
				}
				else if (this.tabControl1.SelectedIndex == 0)
				{
					result = Convert.ToDouble(this.hyTxtCalcDepth.Text);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = 0.0;
			}
			return result;
		}

		public int GetCalcTypeNum()
		{
			int result = 1;
			if (this.tabControl1.SelectedIndex == 1)
			{
				result = 3;
			}
			else if (this.rdoPreventDewing.Checked)
			{
				result = 2;
			}
			return result;
		}

		private double GetSpecTemp(System.Windows.Forms.TextBox txtBox)
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(txtBox.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = 0.0;
			}
			return result;
		}

		private void rdoKeepWarm_CheckedChanged(object sender, EventArgs e)
		{
			this.InitialInterfaceControlStatue();
			RadioButton radioButton = sender as RadioButton;
			if (radioButton == null)
			{
				return;
			}
			if (radioButton.Text == "无采暖")
			{
				LocationWeather weatherByLocationName = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
				this.hyTxtDesignTemp.Text = weatherByLocationName.AverageTemp.ToString();
				this.hyTxtDesignTemp.ReadOnly = true;
			}
			else if (radioButton.Text == "有采暖")
			{
				this.hyTxtDesignTemp.ReadOnly = false;
			}
			if (radioButton.Text == "防结露")
			{
				if (this.rdoNoAirCondition.Checked)
				{
					LocationWeather weatherByLocationName2 = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
					this.hyTxtDesignTemp.Text = weatherByLocationName2.ConditionTemp.ToString();
					this.hyTxtDesignTemp.ReadOnly = true;
					this.hyTxtDewingTemp.Text = weatherByLocationName2.DewPointTemp.ToString();
					this.hyTxtDewingTemp.ReadOnly = true;
				}
				else
				{
					this.hyTxtDesignTemp.ReadOnly = false;
					this.hyTxtDewingTemp.ReadOnly = false;
				}
			}
			if (radioButton.Text == "保温")
			{
				if (this.rdoNoHeating.Checked)
				{
					LocationWeather weatherByLocationName3 = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
					this.hyTxtDesignTemp.Text = weatherByLocationName3.AverageTemp.ToString();
					this.hyTxtDesignTemp.ReadOnly = true;
				}
				else
				{
					this.hyTxtDesignTemp.ReadOnly = false;
				}
			}
			if (radioButton.Text == "无空调")
			{
				LocationWeather weatherByLocationName4 = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
				this.hyTxtDesignTemp.Text = weatherByLocationName4.ConditionTemp.ToString();
				this.hyTxtDesignTemp.ReadOnly = true;
				this.hyTxtDewingTemp.Text = weatherByLocationName4.DewPointTemp.ToString();
				this.hyTxtDewingTemp.ReadOnly = true;
				return;
			}
			if (radioButton.Text == "有空调")
			{
				this.hyTxtDesignTemp.ReadOnly = false;
				this.hyTxtDewingTemp.ReadOnly = false;
			}
		}

		private Dictionary<string, string> GetInterfaceValue()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("TabIndex", this.tabControl1.SelectedIndex.ToString());
			dictionary.Add("CalcType", this.rdoPreventDewing.Checked ? this.rdoPreventDewing.Text : this.rdoKeepWarm.Text);
			string text = this.rdoNoHeating.Text;
			if (this.rdoHeating.Checked)
			{
				text = this.rdoHeating.Text;
			}
			dictionary.Add("Heating", text);
			string text2 = this.rdoNoAirCondition.Text;
			if (this.rdoAirCondition.Checked)
			{
				text2 = this.rdoAirCondition.Text;
			}
			dictionary.Add("Dewing", text2);
			dictionary.Add("LocationName", this.btnLocation.Text);
			dictionary.Add("InsulationMaterial", this.cmbInsulationMaterial.Text);
			dictionary.Add("MediumTemp", this.hyTxtMediumTemp.Text);
			dictionary.Add("DesignTemp", this.hyTxtDesignTemp.Text);
			dictionary.Add("DewingTem", this.hyTxtDewingTemp.Text);
			dictionary.Add("Thickness", this.hyTxtDefineDepth.Text);
			return dictionary;
		}

		private double GetDoubleByString(string txtValue)
		{
			double result = 0.0;
			if (string.IsNullOrEmpty(txtValue))
			{
				return result;
			}
			try
			{
				result = Convert.ToDouble(txtValue);
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private void InitialInterfaceControlStatue()
		{
			if (this.rdoPreventDewing.Checked)
			{
				this.pnlAirCondition.Visible = true;
				this.pnlHeating.Visible = false;
				this.hyTxtMediumTemp.Text = "15";
			}
			else
			{
				this.pnlAirCondition.Visible = false;
				this.pnlHeating.Visible = true;
				this.hyTxtMediumTemp.Text = "30";
			}
			if (this.pnlHeating.Visible)
			{
				if (this.rdoNoHeating.Checked)
				{
					this.lblAverageTemp.Visible = true;
					this.lblHuangjingTemp.Visible = false;
					this.lblHeatingTemp.Visible = false;
					this.lblDesignDewingTemp.Visible = false;
					this.lblACDesignTemp.Visible = false;
					this.lblACDewingTemp.Visible = false;
					this.lblAirConditionTemp.Visible = false;
					this.hyTxtDesignTemp.ReadOnly = true;
					this.hyTxtDewingTemp.Visible = false;
				}
				else if (this.rdoHeating.Checked)
				{
					this.lblAverageTemp.Visible = false;
					this.lblHuangjingTemp.Visible = false;
					this.lblHeatingTemp.Visible = true;
					this.lblDesignDewingTemp.Visible = false;
					this.lblACDesignTemp.Visible = false;
					this.lblACDewingTemp.Visible = false;
					this.lblAirConditionTemp.Visible = false;
					this.hyTxtDesignTemp.ReadOnly = false;
					this.hyTxtDewingTemp.Visible = false;
				}
			}
			else if (this.pnlAirCondition.Visible)
			{
				if (this.rdoNoAirCondition.Checked)
				{
					this.lblAverageTemp.Visible = false;
					this.lblHuangjingTemp.Visible = false;
					this.lblHeatingTemp.Visible = false;
					this.lblDesignDewingTemp.Visible = false;
					this.lblACDesignTemp.Visible = true;
					this.lblACDewingTemp.Visible = true;
					this.lblAirConditionTemp.Visible = false;
					this.hyTxtDewingTemp.Visible = true;
					this.hyTxtDesignTemp.ReadOnly = false;
				}
				else if (this.rdoAirCondition.Checked)
				{
					this.lblAverageTemp.Visible = false;
					this.lblHuangjingTemp.Visible = false;
					this.lblHeatingTemp.Visible = false;
					this.lblDesignDewingTemp.Visible = true;
					this.lblACDesignTemp.Visible = false;
					this.lblACDewingTemp.Visible = false;
					this.lblAirConditionTemp.Visible = true;
					this.hyTxtDewingTemp.Visible = true;
					this.hyTxtDesignTemp.ReadOnly = true;
				}
			}
			try
			{
				Convert.ToDouble(this.hyTxtMediumTemp.Text);
				Convert.ToDouble(this.hyTxtDesignTemp.Text);
				Convert.ToDouble(this.hyTxtDewingTemp.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				return;
			}
			this.CalAndSetInsulationThickness();
		}

		private void SetPanelVisible(List<Panel> listShowPanel)
		{
			foreach (object obj in base.Controls)
			{
				if (obj is Panel)
				{
					Panel panel = obj as Panel;
					panel.Visible = false;
					if (listShowPanel.Contains(panel))
					{
						panel.Visible = true;
					}
				}
			}
		}

		private void FormPipeInsulationLayer_Load(object sender, EventArgs e)
		{
			this.operationLocation = new OperationLocation("");
			this.useSetting = new HVACUserSetting();
			this.dicInterface = this.useSetting.GetCalcDuctInsulationThickness();
			this.BindingCombox(this.dicInterface["InsulationMaterial"]);
			this.InitialInterface();
			this.rdoKeepWarm.Checked = true;
			this.hyTxtCalcDepth.ReadOnly = true;
			this.beLoad = false;
		}

		private void SelectRadioButton(RadioButton rdoButton)
		{
			rdoButton.PerformClick();
			rdoButton.Select();
			rdoButton.Checked = true;
		}

		private void BindingCombox(string selectMaterial)
		{
			List<InsulationLayerParam> allInsulationMaterials = this.operationLocation.GetAllInsulationMaterials();
			this.BindingCombox(allInsulationMaterials, this.cmbDefineInsulationMaterial, selectMaterial);
			this.BindingCombox(allInsulationMaterials, this.cmbInsulationMaterial, selectMaterial);
		}

		private void InitialInterface()
		{
			this.pnlInsulationMaterial.Visible = true;
			this.pnlMediumLocation.Visible = true;
			string text = this.dicInterface["CalcType"];
			if (text.Equals("防结露"))
			{
				this.pnlAirCondition.Visible = true;
				this.pnlHeating.Visible = false;
				this.rdoPreventDewing.Checked = true;
				this.rdoKeepWarm.Checked = false;
			}
			else
			{
				this.pnlAirCondition.Visible = false;
				this.pnlHeating.Visible = true;
				this.rdoPreventDewing.Checked = false;
				this.rdoKeepWarm.Checked = true;
			}
			string text2 = this.dicInterface["Heating"];
			if (this.rdoHeating.Text == text2)
			{
				this.rdoHeating.Checked = true;
			}
			else
			{
				this.rdoNoHeating.Checked = true;
			}
			string text3 = this.dicInterface["Dewing"];
			if (this.rdoAirCondition.Text == text3)
			{
				this.rdoAirCondition.Checked = true;
			}
			else
			{
				this.rdoNoAirCondition.Checked = true;
			}
			string text4 = this.dicInterface["MediumTemp"];
			string text5 = this.dicInterface["DesignTemp"];
			string text6 = this.dicInterface["DewingTem"];
			string text7 = this.dicInterface["Thickness"];
			try
			{
				this.hyTxtMediumTemp.Text = text4;
				this.hyTxtDesignTemp.Text = text5;
				this.hyTxtDewingTemp.Text = text6;
				this.hyTxtDefineDepth.Text = text7;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw;
			}
			if (this.pnlHeating.Visible && this.rdoKeepWarm.Checked)
			{
				if (this.rdoNoHeating.Checked)
				{
					this.hyTxtDesignTemp.ReadOnly = true;
				}
				else if (this.rdoHeating.Checked)
				{
					this.hyTxtDesignTemp.ReadOnly = false;
				}
			}
			if (this.pnlAirCondition.Visible && this.rdoPreventDewing.Checked)
			{
				if (this.rdoNoAirCondition.Checked)
				{
					this.hyTxtDesignTemp.ReadOnly = true;
					this.hyTxtDewingTemp.ReadOnly = true;
				}
				else if (this.rdoAirCondition.Checked)
				{
					this.hyTxtDesignTemp.ReadOnly = false;
					this.hyTxtDewingTemp.ReadOnly = false;
				}
			}
			string text8 = this.dicInterface["LocationName"];
			this.btnLocation.Text = text8;
			string text9 = this.dicInterface["TabIndex"];
			this.tabControl1.SelectedIndex = 0;
			if (text.Equals("保温") && text2.Equals("有采暖"))
			{
				this.SelectRadioButton(this.rdoHeating);
			}
			if (text.Equals("防结露") && text3.Equals("无空调"))
			{
				this.SelectRadioButton(this.rdoNoAirCondition);
			}
		}

		private void BindingCombox(List<InsulationLayerParam> listMaterial, System.Windows.Forms.ComboBox combox, string selectedMaterial)
		{
			for (int i = 0; i < listMaterial.Count; i++)
			{
				combox.Items.Insert(i, listMaterial[i].Name);
			}
			combox.Tag = listMaterial;
			combox.SelectedItem = selectedMaterial;
		}

		private void btnLocation_Click(object sender, EventArgs e)
		{
			FormObtainLocation formObtainLocation = new FormObtainLocation(this.btnLocation.Text);
			if (formObtainLocation.ShowDialog() == DialogResult.OK)
			{
				this.btnLocation.Text = formObtainLocation.GetSelectedLocation();
			}
		}

		private void btnLocation_TextChanged(object sender, EventArgs e)
		{
			LocationWeather weatherByLocationName = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
			if (this.pnlHeating.Visible && this.rdoNoHeating.Checked)
			{
				this.hyTxtDesignTemp.Text = weatherByLocationName.AverageTemp.ToString();
			}
			if (this.pnlAirCondition.Visible && this.rdoNoAirCondition.Checked)
			{
				this.hyTxtDesignTemp.Text = weatherByLocationName.ConditionTemp.ToString();
				this.hyTxtDewingTemp.Text = weatherByLocationName.DewPointTemp.ToString();
			}
		}

		private void FormPipeInsulationLayer_FormClosing(object sender, FormClosingEventArgs e)
		{
			Dictionary<string, string> interfaceValue = this.GetInterfaceValue();
			this.useSetting.WriteCalcDuctInsulationThickness(interfaceValue);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			FormDuctInsulationLayer.SendMessage(CreatDuctInsulation.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormDuctInsulationLayer.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormDuctInsulationLayer.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void FormPipeInsulationLayer_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		private void FormPipeInsulationLayer_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.ChangeOperateState();
			this.ChangeOperateState();
		}

		private void hyTxtDewingTemp_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox == null || this.beLoad)
			{
				return;
			}
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			try
			{
				num = Convert.ToDouble(this.hyTxtMediumTemp.Text);
				num2 = Convert.ToDouble(this.hyTxtDesignTemp.Text);
				num3 = Convert.ToDouble(this.hyTxtDewingTemp.Text);
			}
			catch (Exception ex)
			{
				num = 0.0;
				num2 = 0.0;
				num3 = 0.0;
				ex.ToString();
			}
			double num4 = 0.001;
			if (this.pnlHeating.Visible && Math.Abs(num - num2) < num4)
			{
				YJKMessageBox.Warning("填入温度值不能相同！");
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
			if (this.pnlAirCondition.Visible && (Math.Abs(num - num2) < num4 || Math.Abs(num - num3) < num4 || Math.Abs(num2 - num3) < num4))
			{
				YJKMessageBox.Warning("填入温度值不能相同！");
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
			this.CalAndSetInsulationThickness();
		}

		private void CalAndSetInsulationThickness()
		{
			if (this.tabControl1.TabIndex != 0)
			{
				return;
			}
			InsulationLayerParam settingMaterial = this.GetSettingMaterial();
			if (settingMaterial == null)
			{
				return;
			}
			new DuctInsulationCalc();
			double designTemp = -1.0;
			double dewingTemp = -1.0;
			if (this.pnlAirCondition.Visible)
			{
				try
				{
					double mediumTemp = Convert.ToDouble(this.hyTxtMediumTemp.Text);
					if (this.rdoNoAirCondition.Checked)
					{
						designTemp = Convert.ToDouble(this.hyTxtDesignTemp.Text);
						dewingTemp = Convert.ToDouble(this.hyTxtDewingTemp.Text);
					}
					if (this.rdoAirCondition.Checked)
					{
						designTemp = Convert.ToDouble(this.hyTxtDewingTemp.Text);
						dewingTemp = Convert.ToDouble(this.hyTxtDesignTemp.Text);
					}
					int num = new DuctInsulationCalc
					{
						MediumTemp = mediumTemp,
						DesignTemp = designTemp,
						DewingTemp = dewingTemp,
						InsulationMaterial = settingMaterial
					}.CalDewingInsulationThickness();
					this.hyTxtCalcDepth.Text = num.ToString();
				}
				catch (Exception ex)
				{
					string message = ex.Message;
					throw;
				}
			}
			if (this.pnlHeating.Visible)
			{
				try
				{
					double mediumTemp = Convert.ToDouble(this.hyTxtMediumTemp.Text);
					designTemp = Convert.ToDouble(this.hyTxtDesignTemp.Text);
					int num = new DuctInsulationCalc
					{
						MediumTemp = mediumTemp,
						DesignTemp = designTemp,
						InsulationMaterial = settingMaterial
					}.CalHeatingInsulationThickness();
					this.hyTxtCalcDepth.Text = num.ToString();
				}
				catch (Exception ex2)
				{
					string message2 = ex2.Message;
					throw;
				}
			}
		}

		private OperationLocation operationLocation;

		private HVACUserSetting useSetting;

		private Dictionary<string, string> dicInterface;

		private bool beLoad = true;
	}
}
